//预加载
function loading(){
	var loading = document.querySelector(".loading");
	var web = document.querySelector(".web");
			//实例化一个预加载资源的对象
			var queue = new createjs.LoadQueue();
			//设置加载的资源
			queue.loadManifest([
				{"id":1,"src":"img/1.png"},
				{"id":2,"src":"img/2.png"},
				{"id":3,"src":"img/3.png"},
				{"id":4,"src":"img/4.png"},
				{"id":5,"src":"img/again.png"},
				{"id":6,"src":"img/bgmBtn.png"},
				{"id":7,"src":"img/css_sprites.png"},
				{"id":8,"src":"img/gainScore.png"},
				{"id":9,"src":"img/game_rule.png"},
				{"id":10,"src":"img/gameBg.jpg"},
				{"id":11,"src":"img/gameCloud.png"},
				{"id":12,"src":"img/gameRank.png"},
				{"id":13,"src":"img/gameRule.png"},
				{"id":14,"src":"img/gang1.png"},
				{"id":15,"src":"img/gang2.png"},
				{"id":16,"src":"img/Go.png"},
				{"id":17,"src":"img/heart.png"},
				{"id":18,"src":"img/indexBg.jpg"},
				{"id":19,"src":"img/loadingBg.png"},
				{"id":20,"src":"img/logo.png"},
				{"id":21,"src":"img/logo2.png"},
				{"id":22,"src":"img/lookRank.png"},
				{"id":23,"src":"img/overBanner.png"},
				{"id":24,"src":"img/overBaoming.png"},
				{"id":25,"src":"img/overRank.png"},
				{"id":26,"src":"img/overShare.png"},
				{"id":27,"src":"img/pigdan.png"},
				{"id":28,"src":"img/playBtn.png"},
				{"id":29,"src":"img/ranklistpic.png"},
				{"id":30,"src":"img/ruleBtn.png"},
				{"id":31,"src":"img/ruleClose.png"},
				{"id":32,"src":"img/score1.png"},
				{"id":33,"src":"img/score2.png"},
				{"id":34,"src":"img/score3.png"},
				{"id":35,"src":"img/score4.png"},
				{"id":36,"src":"img/score5.png"},
				{"id":37,"src":"img/score6.png"},
				{"id":38,"src":"img/score7.png"},
				{"id":39,"src":"img/shareWeb.png"},
				{"id":40,"src":"img/star.png"},
				{"id":41,"src":"img/winnersListBtn.png"},
				{"id":42,"src":"music/bgm.mp3"}
			])
			queue.on("complete",function(res){//加载完成后触发
				loading.style.display = "none";
				web.style.display = "block";
			})
			queue.on("progress",function(res){//加载资源过程中触发
				console.log(res);
				loading.innerHTML = "<p>"+Math.floor(res.progress*100)+"%</p>";
			})
}
loading();
//首页按键功能
function webBtn(){
	$(".gameRule").on("click",function(){
		$(".gameContent").css("transform","scale(1)");
	})
	$(".ruleClose").on("click",function(){
		$(".gameContent").css("transform","scale(0)");
	})
	$(".lookRank").on("touchstart",function(){
		$(".gameContent").css("transform","scale(0)");
		$(".gameRank").css("transform","scale(1)");
	})
	$(".winners").on("touchstart",function(){
		$(".gameRank").css("transform","scale(1)");
	})
	$(".rankClose").on("touchstart",function(){
		$(".gameRank").css("transform","scale(0)");
	})
	$(".go").on("touchend",function(){
		$(".web").css("display","none");
		$(".gameWeb").css("display","block");
		startGame();
	})
	$(".overRank").on("touchstart",function(){
		$(".gameRank").css("transform","scale(1)");
	})
	$(".overShare").on("touchstart",function(){
		$(".shareWeb").css("transform","translateX(0)");
	})
	$(".backBtn").on("touchstart",function(){
		$(".shareWeb").css("transform","translateX(-100%)");
	})
}
webBtn();
//游戏开始
$(".start").on("touchend",function(){
	$(".web").css("display","none");
	$(".gameWeb").css("display","block");
	startGame();
})
function startGame(){
	function bgAnimate(sel1,sel2,aniTime){
	$(sel1).animate({"left":-$(sel2).width()},aniTime,"linear",function(){
		$(sel1).css("left",0);
		bgAnimate(sel1,sel2,aniTime);
	});
	
}
//主背景
bgAnimate(".bgBox",".gameBg img",20000);
//云1
bgAnimate(".gameCloud1",".gameCloud1 .cloud img",15000);
//云2
bgAnimate(".gameCloud2",".gameCloud2 .cloud img",10000);
//碰撞函数
function bump(obj1,obj2){
	//offsetLeft,offsetTop是相对于其祖先元素中
	//具有定位信息的那个元素的左上角的位置
				
	//想要获取元素到浏览器左上角的位置
	//getBoundingClientRect()
	let obj1Pos = obj1.getBoundingClientRect();
	let obj2Pos = obj2.getBoundingClientRect();
	let t1 = obj1.offsetTop,
		r1 = obj1Pos.right,
		b1 = obj1Pos.bottom,
		l1 = obj1.offsetLeft;
	let t2 = obj2.offsetTop,
		r2 = obj2Pos.right,
		b2 = obj2Pos.bottom,
		l2 = obj2.offsetLeft;
	if(t1>b2||r1<l2||b1<t2||l1>r2){
		return false;
	}else{
		return true;
	}
}
var lifeTimer;
//猪丹累了
function pigDrop(){
	var winsH = $("html,body").height();
	var pigH = $(".startR").height();
	var pigT = $(".startR").position().top;
	var heart = 3;
	lifeTimer = setInterval(life,500);
	$(".startR img").removeClass("pigRun");
	$(".startR img").css("left","-600%");
	$(".startR").animate({"top":winsH},{
		easing: "linear",
		duration:1000,
		step:function(now,fx){
			var brg2 = document.querySelector(".bridge2"),
				brg3 = document.querySelector(".bridge3");
			var pigdan = document.querySelector(".startR");
			var isRun1 = bump(pigdan,brg2),
				isRun2 = bump(pigdan,brg3);
			if(isRun1){
				$(".startR").stop(true,false);
				$(".startR img").addClass("pigRun");
				console.log(fx.now);
				jump = 0;
				var runDown = setInterval(function(){
						if($(".bridge2").width()+$(".bridge2").offset().left<$(".startR").offset().left){
							$(".startR").animate({
								top:$(window).height()
							},{
								easing: 'linear',
			    				duration: 800
							});
							clearInterval(runDown);
						}
					})
			}
			if(isRun2){
				$(".startR").stop(true,false);
				$(".startR img").addClass("pigRun");
				console.log(fx.now);
				jump = 0;
				var runDown = setInterval(function(){
						if($(".bridge3").width()+$(".bridge3").offset().left<$(".startR").offset().left){
							$(".startR").animate({
								top:$(window).height()
							},{
								easing: 'linear',
			    				duration: 800
							});
							clearInterval(runDown);
						}
					})
			}
		}
	});
	console.log(1);
}
//猪丹跳
var jump = 0;
function pigJump(){
	var winsH = $("html,body").height();
	$(window).on("touchstart",function(event){
		$(".startR img").removeClass("pigRun");
		$(".startR img").css("left","-600%");
		console.log("jump",jump);
		var pigT= $(".startR").position().top;//实时获取猪丹上方定位
		var pigH = $(".startR").height();//实时获取猪丹的高度
		jump = jump+1;
		if(jump<3){
			console.log(44);
			$(".startR").stop(true,false);//暂停下降动画
			$(".startR").animate({"top":pigT-pigH},500);//每点击一次猪丹上升的高度
			pigDrop();
		}
	})
}
pigJump();
var lifeNum = 3;
function life(){
	var winsH = $("html,body").height();
	var pigH = $(".startR").height();
	var pigT = $(".startR").position().top;
	var heart = document.querySelectorAll(".heart");
	if(pigT>=winsH){
		lifeNum--;
		$(".heart").eq(0).remove();
		$(".startR").css("top",-pigH);
		clearInterval(lifeTimer);
		pigDrop();
	}
	if(lifeNum<=0){
		console.log(1)
		$(".overWeb").css("display","block");
		$(".gameWeb").css("display","none");
		}
	
}

//桥移动函数
function bridge1(spoT){
	//var brg1L = $(".bridge1").position().left;
	var winsW = $(window).width();
	var brg1W = $(".bridge1").width();
	var brg2W = $(".bridge2").width();
	var pigW = $(".startR").width();
	var brg2L = $(".bridge2").position().left;
	$(".bridge1").animate({"left":-brg1W},3000,"linear");
}
function bridge2(ranN){
	var winsW = $(window).width();
	var brg1W = $(".bridge1").width();
	var brg2W = $(".bridge2").width();
	var pigW = $(".startR").width();
	var brg2L = $(".bridge2").position().left;
		$(".bridge2").animate({"left":-brg2W},4000,"linear",function(){
			$(".bridge2").find(".scoreGift").remove();
			$(".bridge2").css({
				"left":winsW,
				"top": 5*ranN+"%"
			});
			gift2();
		});
		setTimeout(function(){
				bridge3();
			},3000)
}
function bridge3(ranN){
	var winsW = $(window).width();
	var brg1W = $(".bridge1").width();
	var brg2W = $(".bridge2").width();
	var brg2H = $(".bridge2").height();
	var pigW = $(".startR").width();
	var	ranN =  Math.ceil(Math.random() * 4+7);
	var brg2L = $(".bridge2").position().left;
		$(".bridge3").animate({"left":-brg2W},4000,"linear",function(){
			$(".bridge3").find(".scoreGift").remove();
			$(".bridge3").css({
				"left":winsW,
				"top": 5*ranN+"%"
			});
			gift3();
		});
		setTimeout(function(){
				bridge2();
		},3000)
}
bridge1();
setTimeout(function(){
	bridge2();
},2000)

//生成分数
function gift2(){
	for(var i=1; i<4; i++){
			var giftDiv = "";
			var giftNum = Math.floor(Math.random()*7+1);
			console.log(giftNum);
			if(giftNum == 7){
				giftDiv = "<img class='stone scoreGift' data-gift='1' style='top:-100%;"+"left:"+(i*50)+"px;' src='img/score"+ giftNum +".png'/>";
				$(".bridge2").append(giftDiv);
			}else{
				var giftHeight = -(Math.ceil(Math.random()*3+6));
			if(giftNum == 6){
				giftDiv = "<img class='gift scoreGift' data-gift='3' style='top:"+giftHeight*100+"%;left:"+((i-1)*125)+"px;' src='img/score"+ giftNum +".png'/>";
				$(".bridge2").append(giftDiv);
			}else{
				giftDiv = "<img class='gift scoreGift' data-gift='2' style='top:"+giftHeight*100+"%;left:"+((i-1)*125)+"px;' src='img/score"+ giftNum +".png'/>";
				$(".bridge2").append(giftDiv);
			}
		}
	}
}
function gift3(){
	for(var i=1; i<4; i++){
			var giftDiv = "";
			var giftNum = Math.floor(Math.random()*7+1);
			if(giftNum == 7){
				giftDiv = "<img class='stone scoreGift' data-gift='1' style='top:-30px;"+"left:"+(i*50)+"px;' src='img/score"+ giftNum +".png'/>";
				$(".bridge3").append(giftDiv);
			}else{
				var giftHeight = -(Math.floor(Math.random()*7+3));
			if(giftNum == 6){
				giftDiv = "<img class='gift scoreGift' data-gift='3' style='top:"+giftHeight*100+"%;left:"+((i-1)*125)+"px;' src='img/score"+ giftNum +".png'/>";
				$(".bridge3").append(giftDiv);
			}else{
				giftDiv = "<img class='gift scoreGift' data-gift='2' style='top:"+giftHeight*100+"%;left:"+((i-1)*125)+"px;' src='img/score"+ giftNum +".png'/>";
				$(".bridge3").append(giftDiv);
			}
		}
	}
}

}
//音乐按钮函数
var isClick = true;
function musicPlay(){
	$(".musicBtn").on("click",function(){
		if(isClick==true){
			$(this).removeClass("musicPlay");
			$(this).children()[0].pause();
			isClick=false;
		}else{
			$(this).addClass("musicPlay");
			$(this).children()[0].play();
			isClick=true;
		}
	})
}
musicPlay();


